package Tests;

import static org.junit.Assert.*;

import javax.swing.DefaultListModel;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Client.PokerClient.src.Client;
import Client.PokerClient.src.GameController;
import Client.PokerClient.src.GameThread;
import Client.PokerClient.src.GameWindow;
import Client.PokerClient.src.ServerController;

public class ServerControllerTest {
  ServerController serverController;
  Client client;
  static Server server;
  
  @BeforeClass
  public static void BServer() {
    server = new Server(4444);
  }
  
  @AfterClass
  public static void AServer() {
    server.EndTest();
  }
  
  @Before
  public void Inicjuj() {
	client = new Client(true);
	client.CreateGame(2, 2, 2, true, false);
	serverController = new ServerController(client);
	client.getGameThread().ConnectToServer("127.0.0.1", "4444");
  }
  
  @Test
  public void NOTNULL() {
    assertNotNull(serverController.getServerDatabase());
  }
  
  @Test
  public void S_REFRESH() {
    String[] ID_GAME_LIST = new String[10];
    DefaultListModel<String> ServerDatabase = new DefaultListModel<String>();
    for(int i=0; i<10; i++) {
      ServerDatabase.addElement("Test: "+i);
      ID_GAME_LIST[i]=Integer.toString(i);
    }
    client.getServerController().S_REFRESH(ID_GAME_LIST, ServerDatabase);
    
    for(int i=0; i<10; i++) {
      assertEquals(client.getServerController().getID_GAME_LIST()[i],ID_GAME_LIST[i]);
   	  assertEquals(client.getServerController().getServerDatabase().getElementAt(i),ServerDatabase.getElementAt(i));
    }
  }
  
  @Test
  public void S_NEWGAME() {
	client.getServerController().S_NEWGAME(4, 2, 1);
	assertEquals(client.getGameController().getActualNumberOfPlayers(),3);
	assertEquals(client.getGameController().getMaxPlayers(),4);
	assertEquals(client.getGameController().getNumberOfBot(),2);
	assertEquals(client.getGameController().isHostPlay(),true);
	
	client.getServerController().S_NEWGAME(2, 1, 0);
	assertEquals(client.getGameController().getActualNumberOfPlayers(),1);
	assertEquals(client.getGameController().getMaxPlayers(),2);
	assertEquals(client.getGameController().getNumberOfBot(),1);
	assertEquals(client.getGameController().isHostPlay(),false);
  }
  
  @Test
  public void S_JOIN() {
    String[] ID_GAME_LIST = {"0", "1", "2", "3"};
    client.getServerController().S_REFRESH(ID_GAME_LIST, client.getServerController().getServerDatabase());
	  
	client.getServerController().getServerDatabase().addElement("1 Game:  Player: 4/4  Boty: 3");
	client.getServerController().getServerDatabase().addElement("2 Game:  Player: 3/4  Boty: 0");
	client.getServerController().getServerDatabase().addElement("3 Game:  Player: 2/2  Boty: 1");
	client.getServerController().getServerDatabase().addElement("4 Game:  Player: 1/4  Boty: 1");
	
	client.getServerController().S_JOIN(0, client.getServerController().getServerDatabase().get(0));
	assertEquals(client.getGameController().getActualNumberOfPlayers(), 4);
	assertEquals(client.getGameController().getMaxPlayers(),4);
	assertEquals(client.getGameController().getNumberOfBot(),3);
	
	client.getServerController().S_JOIN(0, client.getServerController().getServerDatabase().get(1));
	assertEquals(client.getGameController().getActualNumberOfPlayers(), 3);
	assertEquals(client.getGameController().getMaxPlayers(),4);
	assertEquals(client.getGameController().getNumberOfBot(),0);
	
	client.getServerController().S_JOIN(0, client.getServerController().getServerDatabase().get(2));
	assertEquals(client.getGameController().getActualNumberOfPlayers(), 2);
	assertEquals(client.getGameController().getMaxPlayers(),2);
	assertEquals(client.getGameController().getNumberOfBot(),1);
	
	client.getServerController().S_JOIN(0, client.getServerController().getServerDatabase().get(3));
	assertEquals(client.getGameController().getActualNumberOfPlayers(), 1);
	assertEquals(client.getGameController().getMaxPlayers(),4);
	assertEquals(client.getGameController().getNumberOfBot(),1);
  }
  
  
  @After
  public void Clean() {
	client.getMainWindow().dispose();
	client.getGameThread().finalize();
	client = null;
	serverController = null;
  }
}
